package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.YyAchievement;
import com.ruoyi.system.domain.YyTeacher;
import com.ruoyi.system.mapper.YyAchievementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YyStudentMapper;
import com.ruoyi.system.domain.YyStudent;
import com.ruoyi.system.service.IYyStudentService;
import com.ruoyi.common.core.text.Convert;

/**
 * 学生Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@Service
public class YyStudentServiceImpl implements IYyStudentService
{
    @Autowired
    private YyStudentMapper yyStudentMapper;

    @Autowired
    private YyAchievementMapper yyAchievementMapper;

    @Autowired
    private SysUserServiceImpl sysUserService;

    /**
     * 查询学生
     *
     * @param id 学生主键
     * @return 学生
     */
    @Override
    public YyStudent selectYyStudentById(Long id)
    {
        return yyStudentMapper.selectYyStudentById(id);
    }

    /**
     * 查询学生列表
     *
     * @param yyStudent 学生
     * @return 学生
     */
    @Override
    public List<YyStudent> selectYyStudentList(YyStudent yyStudent)
    {
        //根据当前userid 获取 角色
        //如 家长角色就 获取自己孩子的信息
        if (yyStudent.getUserId() != null){
            Integer roleId = yyAchievementMapper.getRoleIdByUserId(yyStudent.getUserId());
            if (roleId != null){
                yyStudent.setRoleId(roleId);
            }
            yyStudent.setpId(yyStudent.getUserId());
        }

        return yyStudentMapper.selectYyStudentList(yyStudent);
    }

    public List<YyStudent> selectYyStudentByIdList(Long id){
        List<YyStudent> YyTeacherByIdList = yyStudentMapper.selectYyStudentByIdList(id);
        YyStudent yyStudent = new YyStudent();
        List<YyStudent> YyTeacherListAll = yyStudentMapper.selectYyStudentList(yyStudent);
        for (YyStudent YyTeacherAll : YyTeacherListAll)
        {
            for (YyStudent YyTeacherById : YyTeacherByIdList)
            {
                if (YyTeacherById.getId().longValue() == YyTeacherAll.getId().longValue())
                {
                    YyTeacherAll.setFlag(true);
                    break;
                }
            }
        }
        return YyTeacherListAll;
    }

    /**
     * 新增学生
     *
     * @param yyStudent 学生
     * @return 结果
     */
    @Override
    public int insertYyStudent(YyStudent yyStudent, SysUser sysUser) throws  Exception  {

        //新增 系统用户
        sysUser.setStatus("0");
        Long[] array = {3L};
        sysUser.setRoleIds(array);
        sysUserService.insertUser(sysUser);
        yyStudent.setUserId(sysUser.getUserId());

        //判断宿舍 不能超过4人
        int rommStudentSum = yyStudentMapper.getStudentSumByRoomId(yyStudent);

        if (rommStudentSum >= 4){
            throw new Exception(StringUtils.format("当前宿舍已住满4人！"));
        }

        int a = yyStudentMapper.insertYyStudent(yyStudent);

        //一个学生多门课程
        String[] split = yyStudent.getCourseId().split(",");
        for (String s : split) {
            YyAchievement y = new YyAchievement();
            y.setAchievement(0L);
            y.setStuId(yyStudent.getId());
            y.setCourseId(Long.valueOf(s));
            yyAchievementMapper.insertYyAchievement(y);
        }

        return a;
    }

    /**
     * 修改学生
     *
     * @param yyStudent 学生
     * @return 结果
     */
    @Override
    public int updateYyStudent(YyStudent yyStudent) throws Exception {
        //判断宿舍 不能超过4人
        int rommStudentSum = yyStudentMapper.getStudentSumByRoomId(yyStudent);

        if (rommStudentSum >= 4){
            throw new Exception(StringUtils.format("当前宿舍已住满4人！"));
        }

        int i = yyStudentMapper.updateYyStudent(yyStudent);
        yyAchievementMapper.deleteYyAchievementById(yyStudent.getId());
        String[] split = yyStudent.getCourseId().split(",");
        for (String s : split) {
            YyAchievement y = new YyAchievement();
            y.setAchievement(0L);
            y.setStuId(yyStudent.getId());
            y.setCourseId(Long.valueOf(s));
            yyAchievementMapper.insertYyAchievement(y);
        }
        return i;
    }

    /**
     * 批量删除学生
     *
     * @param ids 需要删除的学生主键
     * @return 结果
     */
    @Override
    public int deleteYyStudentByIds(String ids)
    {
        yyAchievementMapper.deleteYyAchievementByIds(Convert.toStrArray(ids));
        return yyStudentMapper.deleteYyStudentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生信息
     *
     * @param id 学生主键
     * @return 结果
     */
    @Override
    public int deleteYyStudentById(Long id)
    {
        yyAchievementMapper.deleteYyAchievementById(id);
        return yyStudentMapper.deleteYyStudentById(id);
    }
}
